package net.wanho.zx.zx_cms.service;


import javax.annotation.Resource;
import net.wanho.zx.pojo.cms.CmsConfig;
import net.wanho.zx.pojo.cms.CmsPage;
import net.wanho.zx.pojo.cms.CmsTemplate;
import net.wanho.zx.zx_cms.dao.CmsConfigRepository;
import net.wanho.zx.zx_cms.dao.CmsPageRepository;

import net.wanho.zx.zx_cms.dao.CmsTempleteRepository;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class CmsService {
    @Resource
    CmsPageRepository cmsPageRepository;

    @Resource
    CmsConfigRepository cmsConfigRepository;

    @Resource
    CmsTempleteRepository cmsPagetemplateRepository;

    public CmsTemplate getCmsTemplateById(String templateid) {
        return this.cmsPagetemplateRepository.findById(templateid).get();
    }

    public CmsConfig getCmsConfigById(String cmscofigid) {
        return this.cmsConfigRepository.findById(cmscofigid).get();
    }

    public Page<CmsPage> queryCmsPages(int pagenum, int pagesize, String siteId) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains()).withIgnorePaths(new String[] { "_class" });
        CmsPage cmsPage = new CmsPage();
        if (null != siteId && !"".equals(siteId.trim()))
            cmsPage.setSiteId(siteId);
        Example<CmsPage> example = Example.of(cmsPage, exampleMatcher);
        PageRequest pageRequest = PageRequest.of(pagenum - 1, pagesize);
        Page<CmsPage> cmsPages = this.cmsPageRepository.findAll(example, (Pageable)pageRequest);
        return cmsPages;
    }

    public void addCmspage(CmsPage cmsPage) {
        this.cmsPageRepository.save(cmsPage);
    }

    public CmsPage getPageById(String pageId) {
        return this.cmsPageRepository.findById(pageId).get();
    }

    public void upadteCmspage(CmsPage cmsPage) {
        this.cmsPageRepository.save(cmsPage);
    }
}
